import requests
import os

url="http://img.plantphoto.cn/image2/b/4034846.jpg"
#url = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
root = "R://python//"
path = root + url.split("/")[-1]
try:
    if not os.path.exists(root):
        os.mkdir(root)
    if not os.path.exists(path):
        #login_res = requests.post(


        #Referer 是重点，但只需要plantphoto即可，不需要全部。/tu/4034846
        headers = {'Host': 'img.plantphoto.cn',
                   'Connection': 'keep-alive',
                   'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36',
                   'DNT': '1',
                   'Referer': 'http://www.plantphoto.cn',
                   'Accept-Encoding': 'gzip, deflate, sdch',
                   'Accept-Language': 'zh-CN,zh;q=0.8,ja;q=0.6'
                   }

        r = requests.get(url, headers = headers)
        r.raise_for_status()
        #使用with语句可以不用自己手动关闭已经打开的文件流
        with open(path,"wb") as f: #开始写文件，wb代表写二进制文件
            f.write(r.content)
        print("爬取完成")
    else:
        print("文件已存在")
except Exception as e:
    print("爬取失败:"+str(e))